package com.ruoyi.web.controller.contest.admin;

import com.ruoyi.activity.service.IContestService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.model.contest.Contest;
import com.ruoyi.model.contest.dto.ContestDto;
import com.ruoyi.model.contest.vo.ContestAdminVo;
import com.ruoyi.model.message.UserMessage;
import com.ruoyi.ucenter.service.IUserMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author : 张怀秋
 **/
@Api(tags = "后台:竞赛接口")
@RestController
@RequestMapping("/admin/contest")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ContestAdminController extends BaseController {
    private final IContestService contestService;

    private final IUserMessageService userMessageService;

    /**
     * 查询竞赛列表 审核
     */
    @PreAuthorize("@ss.hasPermi('contest:contest:list')")
    @GetMapping("/list")
    public TableDataInfo list(Contest webContest) {
        startPage();
        List<Contest> list = contestService.selectWebContestList(webContest);
        return getDataTable(list);

    }

    /**
     * 查询竞赛列表 信息
     * @param webContest
     * @return
     */
    @PreAuthorize("@ss.hasPermi('contest:contest:list')")
    @GetMapping("/list/info")
    public TableDataInfo listInfo(Contest webContest) {
        startPage();
        List<ContestAdminVo> list = contestService.selectWebContestListV2(webContest);
      return getDataTable(list);
    }

    /**
     * 导出竞赛列表
     */
    @PreAuthorize("@ss.hasPermi('contest:contest:export')")
    @Log(title = "竞赛", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Contest webContest) {
        List<Contest> list = contestService.selectWebContestList(webContest);
        ExcelUtil<Contest> util = new ExcelUtil<>(Contest.class);
        util.exportExcel(response, list, "竞赛数据");
    }

    /**
     * 获取竞赛详细信息
     */
    @PreAuthorize("@ss.hasPermi('contest:contest:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(contestService.selectWebContestById(id));
    }

    @PreAuthorize("@ss.hasPermi('contest:contest:query')")
    @PostMapping("/dto")
    public AjaxResult getDtoInfo(@RequestBody String id) {
        return success(contestService.selectWebContestDtoById(id));
    }

    /**
     * 新增竞赛
     */
    @PreAuthorize("@ss.hasPermi('contest:contest:add')")
    @Log(title = "竞赛", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Contest webContest) {
        return toAjax(contestService.insertWebContest(webContest));
    }

    /**
     * 修改竞赛
     */
    @PreAuthorize("@ss.hasPermi('contest:contest:edit')")
    @Log(title = "竞赛", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public AjaxResult edit(@RequestBody ContestDto webContest,
                           @PathVariable String id) {
        return toAjax(contestService.updateWebContest(webContest, id));
    }

    /**
     * 删除竞赛
     */
    @PreAuthorize("@ss.hasPermi('contest:contest:remove')")
    @Log(title = "竞赛", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(contestService.deleteWebContestByIds(ids));
    }


    @ApiOperation("审核竞赛信息")
    @GetMapping("/check/contest/{contestId}/{status}")
    public AjaxResult checkContest(@ApiParam("当前审核的竞赛id") @PathVariable("contestId") String contestId,
                                   @ApiParam("当前的审核状态") @PathVariable("status") String status) {
        return toAjax(this.contestService.checkContestStatus(contestId, status));
    }

    @ApiOperation("审核完成后编辑消息")
    @PostMapping("/send/message")
    public AjaxResult sendMessage(@ApiParam("发送的消息") @RequestBody UserMessage userMessage) {
        return toAjax(this.userMessageService.insertUserMessage(userMessage));
    }

    /**
     * 添加和修改竞赛
     */
    @ApiOperation("简易添加修改")
    @PostMapping("/add/or/update/dto")
    public AjaxResult addOrUpdateContestDto(@RequestBody ContestDto contestDto) {
        return toAjax(this.contestService.addOrUpdateContestSimple(contestDto));
    }

}
